package study;

import java.util.ArrayDeque;
import java.util.Deque;

public class EvalRpn {

    public static int evalRpn(String[] arr) {
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals("+")) {
                stack.push(stack.pop() + stack.pop());
                continue;
            }
            if (arr[i].equals("-")) {
                stack.push(stack.pop() - stack.pop());
                continue;
            }
            if (arr[i].equals("*")) {
                stack.push(stack.pop() * stack.pop());
                continue;
            }
            if (arr[i].equals("/")) {
                stack.push(stack.pop() / stack.pop());
                continue;
            }
            stack.push(Integer.valueOf(arr[i]));
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        String[] arr = {"2", "1", "+", "3", "*"};
        System.out.println(evalRpn(arr));
    }
}
